<?php
class main {

    private $step = null;

    public function __construct() {
        $this->v = C( 'Template' );
        $this->init();
        $this->v->init( APP_PATH . "/themes/install/", APP_TPL . "install/" );
        $this->step = R( "step", 1 );
    }

    public function index( $c, $a ) {
        include $this->v->display( 'index' );
    }

    public function checkDB() {

        foreach ( $_POST as $k => $v ) {
            define( $k, trim( $v ) );
        }

        if ( !$conn = @mysql_connect( dbhost, dbuser, dbpass ) ) {
            exit ( "Can not connect to MySQL server : " . mysql_error() );
        }

        if ( !$db = mysql_select_db( dbname, $conn ) ) {
            exit( "Can\'t use " . dbname . " : " . mysql_error() );
        }

        if ( $query = @mysql_query( "SHOW TABLES FROM " . dbname ) ) {
            while ( $row = mysql_fetch_row( $query ) ) {
                if ( preg_match( "/^" . dbprefix . "/", $row[ 0 ] ) ) {
                    @mysql_close( $conn );
                    exit( 'unique' );
                }
            }
        }

        @mysql_close( $conn );
        exit( 'success' );
    }

    public function install() {

        $config = "<?php \n";
        foreach ( $_POST[ 'db' ] as $k => $v ) {
            define( $k, trim( $v ) );
            $config .= "define('{$k}', '{$v}');\n";
        }

        $APP_KEY = md5( uniqid() );
        $APP_API_KEY = md5( uniqid() );
        $APP_COOKIE = substr( $APP_KEY, 8, 6 );
        $APP_COOKIE_DOMAIN = "." . ltrim( S( "HTTP_HOST" ), "www." );

        $config .= "define('APP_KEY', '" . $APP_KEY . "');\n";
        $config .= "define('APP_API_KEY', '" . $APP_API_KEY . "');\n";
        $config .= "define('APP_COOKIE', '" . $APP_COOKIE . "');\n";
        $config .= "define('APP_COOKIE_DOMAIN', '" . $APP_COOKIE_DOMAIN . "');\n";

        define( "APP_KEY", $APP_KEY );


        $this->create_table( "table.sql" );
        $this->create_table( "date.sql" );
        $this->show( "导入数据    ... 成功" );

        $model = C( "Model" );
        $model->table = "user";
        foreach ( $_POST[ 'user' ] as $k => $v ) {
            $users[ $k ] = trim( $v );
        }
        $users[ 'sid' ] = 1;
        $users[ 'regdate' ] = time();
        $users[ 'regip' ] = S( 'ip' );
        $users[ 'actions' ] = '*';
        $users[ 'pass' ] = md6( $users[ 'pass' ] );
        $model->create( $users ) && $this->show( "创建管理员     ... 成功" );

        $model->Cache()->update() && $this->show( "更新缓存     ... 成功" );

        // 写入数据库配置
        file_put_contents( APP_PATH . "/system/config.php", $config ) && $this->show( "创建配置文件     ... 成功" );
        chmod( APP_PATH . "/system/config.php", 0666 );

        echo "安装完成     ... 成功<br />";
    }

    private function show( $msg ) {
        echo $msg . "<br />";
    }

    private function create_table( $file ) {
        $table_sql = file_get_contents( APP_PATH . '/system/install/' . $file );
        $table_sql = trim( str_replace( 'xx_', dbprefix, $table_sql ) );
        $sqldump = explode( ";\r", $table_sql );

        foreach ( $sqldump as $k => $v ) {
            $sqldump[ $k ] = trim( preg_replace( "/\r/", "", $v ) );
        }

        # 创建数据库
        foreach ( $sqldump as $query ) {
            if ( substr( $query, 0, 12 ) == 'CREATE TABLE' ) {
                preg_match( "/CREATE TABLE `([a-z0-9_]+?)`/is", $query, $m );
                $name = $m[ 1 ];
                $this->show( "创建表: {$name}     ... 成功" );
            }
            C( "Model" )->runSql( trim( $query ) );
        }
        return true;
    }

    private function init() {
        rm( APP_PATH . "/content/cache/", false );
        rm( APP_PATH . "/content/tpl/", false );
        rm( APP_PATH . "/content/log/", false );
    }

    /**
     * 魔术函数，实现对控制器扩展类的自动加载
     */
    public function __call( $name, $args ) {
        return C( $name )->__input( $args );
    }
}